package gl.training.basics;
class CalendarSeeker {

	public static void main(String[] args) {
		String[] dwName = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", 
			"Friday", "Saturday"};
		//----------------------- inputing -------------------------------
		System.out.println("***** Calendar Seeker *****");	
		int cd = requestNumber("day(dd) = ") - 1;
		int cm = requestNumber("month(mm) = ") - 1;		
		int cy = requestNumber("year(yyyy) = ") - 1;
		while(cy < 1980-1) {
			System.out.println("Input a year that is bigger than 1980, please.");
			cy = requestNumber("year(yyyy) = ") - 1;
		}
		
		int d = 0, m = 0, y = 1979, 
			dow = 2; // day of week (Tuesday)
		int[] DIM	= {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

		while((d < cd) || (m < cm) || (y < cy)) {
			d++;
			dow++;
			if(dow > 6) dow = 0;
			if(DIM[m] <= d) { // can't hold
				d = 0;
				m++;
				if(m > 11) {
					m = 0;
					y++;
					if((y+1) % 4 == 0) DIM[1] = 29; else DIM[1] = 28;
				}
			}
		}
			
		System.out.println("Answer: " + dwName[dow]);
		
	}
	
	private static int requestNumber(String msg) {
		boolean repeat;
		int n = 0;
		do {
			repeat = false;
			System.out.print(msg);
			try {
				String str;
				byte[] buf = new byte[16];
				int q = System.in.read(buf, 0, 15);				
				str = new String(buf, 0, q-2);
				n = Integer.parseInt(str);
			} catch(Exception e) {
				System.out.println("Number is incorrect. Try again.");
				repeat = true;
			}		
		}while(repeat);
		return n;
	}
}